
/*This following codes replicate Tables 3-6

Note that
(1). theta2_E_fjt_firm: firm-level emission-output elasticity;
(2). m_theta2_E_fjt_firm: the median of emission-output elasticity in each industry;
(3). diff_theta2_E_fjt_firm: the change in firm-level emission-output elasticity;
(4). m_elasticity: the median change ot emission-output elasticity in each industry.
*/

/*
Also notice that 
prod2code==1: mineral
prod2code==2: electricity generation
prod2code==3: food products
prod2code==4: alcoholic berage
prod2code==5: cloth
prod2code==6: clothing and apparel
prod2code==7: wooden furniture
prod2code==8: paper products
prod2code==11: fuel, diesel & gas
prod2code==14: chemical products
prod2code==16: rubber products
prod2code==18: tires&conveys belts
prod2code==19: plastic products
prod2code==20: glass& ceremic products
prod2code==21: crude steel
prod2code==22: high quality steel&steel plates
prod2code==25: heavy metal products
prod2code==26: light metal products
prod2code==29: auto parts
prod2code==30: wheels, gears&mining quipment
prod2code==35: transportation equipment
prod2code==36: abatement equipment& heavy machine
prod2code==39: common cables&elc. wires
prod2code==43:measuring tech., printer, etc  
*/




*****************************88*************************************
*                                   Table 1
*********************************************************************

* Part I: Single-Technology
**************************************************************
use "D:\Nanjing\2019\decomposition_final\data\Single_technology",clear



*------------------------------------------------------------
*(1) Mean emission-output elasticity: Column 1 of Table 1
*--------------------------------------------------------------
drop if theta2_E_fjt_firm<0
egen ub_E=pctile(theta2_E_fjt_firm),p(97)
egen lb_E=pctile(theta2_E_fjt_firm),p(3)
bysort prod_2: sum theta2_E_fjt_firm if theta2_E_fjt_firm>lb_E&theta2_E_fjt_firm<ub_E   /* mean elasticity*/

**************************************************************************************************
*--------------------------------------------------------------
*(2) Median Column 2 of Table 1
*--------------------------------------------------------------
bysort prod_2: egen m_theta2_E_fjt_firm=pctile(theta2_E_fjt_firm),p(50)	
bysort prod_2: sum  m_theta2_E_fjt_firm





*************************************************************************************************************
*----------------------------------------------------------
* This part is to calculate the change during 2000-2005
*----------------------------------------------------------
use "D:\Nanjing\2019\decomposition_final\data\Single_technology",clear

egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
duplicates drop fid indc_y, force

xtset fid indc_y
tsfill, full
bysort fid: gen MC_fp_l=l.MC_fp                             /*lagged marginal cost*/
bysort fid: gen Markup_firm_l=l.Markup_firm                 /*lagged markup*/
bysort fid: gen theta2_E_fjt_firm_l=l.theta2_E_fjt_firm     /*lagged elasticity of emission*/
bysort fid: gen tax_emi_l=l.tax_emi                        /*lagged implied emission tax*/

bysort fid: gen diff_MC=(MC_fp-MC_fp_l)/MC_fp_l
bysort fid: gen diff_markup=(Markup_firm-Markup_firm_l)/Markup_firm_l
bysort fid: gen diff_theta2_E_fjt_firm=(theta2_E_fjt_firm-theta2_E_fjt_firm_l)/theta2_E_fjt_firm_l
bysort fid: gen diff_tax_emi=(tax_emi-tax_emi_l)/tax_emi_l


* only keep observations in 2005
keep if indc_y==2
bysort prod_2: egen m_elasticity=pctile(diff_theta2_E_fjt_firm),p(50)	
bysort prod_2: egen m_MC=pctile(diff_MC),p(50)	
bysort prod_2: egen m_markup=pctile(diff_markup),p(50)
bysort prod_2: egen m_tax_emi=pctile(diff_tax_emi),p(50)



*------------------------------------------------------------------
* 3. Mean change in emission-output elasticity: Column 3 of Table 1
*------------------------------------------------------------------
capture drop ub_elas lb_elas
egen ub_elas=pctile(diff_theta2_E_fjt_firm),p(97)
egen lb_elas=pctile(diff_theta2_E_fjt_firm),p(3)


bysort prod_2: sum diff_theta2_E_fjt_firm   if diff_theta2_E_fjt_firm>lb_elas&diff_theta2_E_fjt_firm<ub_elas         /*mean change in elasticity*/


*----------------------------------------------------------------------
* 4. Median change in emission-output elasticity: Column 4 of Table 1
*----------------------------------------------------------------------
bysort prod_2: sum m_elasticity







***********************************************Split here***********************************************************
*------------------------------
*Part II: Multiple-Techology
*------------------------------
use "D:\Nanjing\2019\decomposition_final\data\Multiple_technology",clear


gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/


replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/


bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace prod_2=4 if prod_2==9

replace theta2_E_fjt_firm=. if prod_2==4
***************************************************************************************************


*---------------------------------------------
*5. Column 5 of Table 1
*---------------------------------------------
sort fid fup12 year
egen ub_E=pctile(theta2_E_fjt_firm) if theta2_E_fjt_firm>0,p(97)
egen lb_E=pctile(theta2_E_fjt_firm)if theta2_E_fjt_firm>0,p(3)

drop if theta2_E_fjt_firm<lb_E  &(prod_2!=21&prod_2!=22&prod_2!=35&prod_2!=36)
keep if theta2_E_fjt_firm<ub_E

egen ub_E2=pctile(theta2_E_fjt_firm)  if prod_2==21|prod_2==22|prod_2==35|prod_2==36, p(95)
replace ub_E=ub_E2 if ub_E>ub_E2

bysort prod_2: sum theta2_E_fjt_firm if theta2_E_fjt_firm<ub_E   /* mean elasticity*/



*----------------------------------------------
*6 Column 6 of Table 1
*-----------------------------------------------
drop if theta2_E_fjt_firm<0
bysort prod_2: egen m_theta2_E_fjt_firm=pctile(theta2_E_fjt_firm),p(50)	
bysort prod_2: sum  m_theta2_E_fjt_firm   /*median elasticity*/
 





*************************************************************************************************************
*----------------------------------------------------------
*  This part is to calculate the change during 2000-2005
*----------------------------------------------------------
use "D:\Nanjing\2019\decomposition_final\data\Multiple_technology",clear

gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 7. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 8. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace prod_2=4 if prod_2==9
replace theta2_E_fjt_firm=. if prod_2==4

egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
sort fid fup12 indc_y 
duplicates  drop fid indc_y, force

xtset fid indc_y
tsfill, full
bysort fid: gen MC_fp_l=l.MC_fp                             /*lagged marginal cost*/
bysort fid: gen Markup_firm_l=l.Markup_firm                 /*lagged markup*/
bysort fid: gen theta2_E_fjt_firm_l=l.theta2_E_fjt_firm     /*lagged elasticity of emission*/
bysort fid: gen tax_emi_l=l.tax_emi                        /*lagged implied emission tax*/

bysort fid: gen diff_MC=(MC_fp-MC_fp_l)/MC_fp_l
bysort fid: gen diff_markup=(Markup_firm-Markup_firm_l)/Markup_firm_l
bysort fid: gen diff_theta2_E_fjt_firm=(theta2_E_fjt_firm-theta2_E_fjt_firm_l)/theta2_E_fjt_firm_l
bysort fid: gen diff_tax_emi=(tax_emi-tax_emi_l)/tax_emi_l


* only keep observations in 2005
keep if indc_y==2
bysort prod_2: egen m_elasticity=pctile(diff_theta2_E_fjt_firm),p(50)	
bysort prod_2: egen m_MC=pctile(diff_MC),p(50)	
bysort prod_2: egen m_markup=pctile(diff_markup),p(50)
bysort prod_2: egen m_tax_emi=pctile(diff_tax_emi),p(50)




capture drop ub_elas lb_elas
egen ub_elas=pctile(diff_theta2_E_fjt_firm),p(95)
egen lb_elas=pctile(diff_theta2_E_fjt_firm),p(5)

*-------------------
*Colum 7 of Table 1
*--------------------
bysort prod_2: sum diff_theta2_E_fjt_firm   if diff_theta2_E_fjt_firm>lb_elas&diff_theta2_E_fjt_firm<ub_elas         /*mean change in elasticity*/

*-----------------------
* Column 8 of Table 3
*-----------------------
bysort prod_2: sum m_elasticity











*************************************Table 2: emissions taxes***************************************************************88
*************************************Table 2: emissions taxes***************************************************************88
*************************************Table 2: emissions taxes***************************************************************88


* Part I: single technology

use "D:\Nanjing\2019\decomposition_final\data\Multiple_technology",clear

drop if theta2_E_fjt_firm<0
*(1) Column 1 of Table 4
egen ub_tax=pctile(tax_emi),p(97)
egen lb_tax=pctile(tax_emi),p(3)
drop if prod_2code==9
*-----------------------------------------------------------
*1. Mean level of implied emission tax: Column 1 of Table 2
*-----------------------------------------------------------
bysort prod_2: sum tax_emi if tax_emi>lb_tax&tax_emi<ub_tax   /* mean implied emission tax*/


*-----------------------------------------------------------
*2. Median level of implied emission tax: Column 2 of Table 2
*-----------------------------------------------------------
bysort prod_2: egen m_tax_emi=pctile(tax_emi),p(50)  /*firm-year level emission tax*/
bysort prod_2: sum m_tax_emi





******************************************Split here***********************************************************
*----------------------------------------------------------
* This part is to calculate the change during 2000-2005
*----------------------------------------------------------
use "D:\Nanjing\2019\decomposition_data_file\code for submission\Single_technology",clear

*(1) Column 1 of Table 3
egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
duplicates drop fid indc_y, force

xtset fid indc_y
tsfill, full
bysort fid: gen MC_fp_l=l.MC_fp                             /*lagged marginal cost*/
bysort fid: gen Markup_firm_l=l.Markup_firm                 /*lagged markup*/
bysort fid: gen theta2_E_fjt_firm_l=l.theta2_E_fjt_firm     /*lagged elasticity of emission*/
bysort fid: gen tax_emi_l=l.tax_emi                        /*lagged implied emission tax*/

bysort fid: gen diff_MC=(MC_fp-MC_fp_l)/MC_fp_l
bysort fid: gen diff_markup=(Markup_firm-Markup_firm_l)/Markup_firm_l
bysort fid: gen diff_theta2_E_fjt_firm=(theta2_E_fjt_firm-theta2_E_fjt_firm_l)/theta2_E_fjt_firm_l
bysort fid: gen diff_tax_emi=(tax_emi-tax_emi_l)/tax_emi_l


* only keep observations in 2005
keep if indc_y==2
bysort prod_2: egen m_elasticity=pctile(diff_theta2_E_fjt_firm),p(50)	
bysort prod_2: egen m_MC=pctile(diff_MC),p(50)	
bysort prod_2: egen m_markup=pctile(diff_markup),p(50)
bysort prod_2: egen m_tax_emi=pctile(diff_tax_emi),p(50)

*----------------------------------------------------------
* 3. Column 3 of Table 2
*-----------------------------------------------------------
capture drop ub_tax lb_tax
egen ub_tax=pctile(diff_tax_emi),p(97)
egen lb_tax=pctile(diff_tax_emi),p(3)
bysort prod_2: sum diff_tax_emi   if diff_tax_emi>lb_tax&diff_tax_emi<ub_tax         /*mean change in emission tax*/

*----------------------------------------------------------
* 4. Column 4 of Table 2
*-----------------------------------------------------------
bysort prod_2: sum m_tax_emi




****************************************Split Here****************************************************
****************************************Multiple-Technology****************************************************

use "D:\Nanjing\2019\decomposition_data_file\code for submission\Multiple_technology",clear

gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace  tax_emi=. if prod_2==9
drop if theta2_E_fjt_firm<0




egen ub_E=pctile(theta2_E_fjt_firm) if theta2_E_fjt_firm>0,p(97)
egen lb_E=pctile(theta2_E_fjt_firm)if theta2_E_fjt_firm>0,p(3)

bysort prod_2: egen ub_E2=pctile(theta2_E_fjt_firm)  if prod_2==21|prod_2==22|prod_2==35|prod_2==36, p(99)
replace ub_E=ub_E2 if ub_E>ub_E2
*drop if theta2_E_fjt_firm>ub_E2 & prod_2==21|prod_2==22|prod_2==35|prod_2==36
*****************************************************************************************************


*---------------------------------
*5. Column 5 of Table 2
*---------------------------------
egen ub_tax=pctile(tax_emi),p(97)
egen lb_tax=pctile(tax_emi),p(3)
drop if theta2_E_fjt_firm>ub_E2 & (prod_2==21|prod_2==22|prod_2==35|prod_2==36)
drop if tax_emi<lb_tax|tax_emi>ub_tax /* &(prod_2!=21&prod_2!=22&prod_2!=35&prod_2!=36)*/
bysort prod_2: sum tax_emi   /* mean implied emission tax*/


*---------------------------------
*6. Column 6 of Table 2
*---------------------------------
bysort prod_2: egen m_tax_emi=pctile(tax_emi),p(50)  /*firm-year level emission tax*/
bysort prod_2: sum m_tax_emi




**************************************************Split here*********************************************************
*----------------------------------------------------------
*  This part is to calculate the change during 2000-2005
*----------------------------------------------------------

use "D:\Nanjing\2019\decomposition_data_file\code for submission\Multiple_technology",clear

bysort fid year: egen MC_fp=sum(c_x_new*expenditure_m_fjt_x/(theta2_fjt_x*quantity)) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
*replace prod_2=25 if prod_2==9
drop if theta2_E_fjt_firm<0
*(1) Column 1 of Table 3
egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
*keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
sort fup12 indc_y
duplicates drop fid indc_y, force

xtset fid indc_y
tsfill, full
bysort fid: gen MC_fp_l=l.MC_fp                             /*lagged marginal cost*/
bysort fid: gen Markup_firm_l=l.Markup_firm                 /*lagged markup*/
bysort fid: gen theta2_E_fjt_firm_l=l.theta2_E_fjt_firm     /*lagged elasticity of emission*/
bysort fid: gen tax_emi_l=l.tax_emi                        /*lagged implied emission tax*/
bysort fid: egen MC_fp_m=mean(MC_fp)

bysort fid: gen diff_MC=(MC_fp-MC_fp_l)/MC_fp_m
bysort fid: gen diff_markup=(Markup_firm-Markup_firm_l)/Markup_firm_l
bysort fid: gen diff_theta2_E_fjt_firm=(theta2_E_fjt_firm-theta2_E_fjt_firm_l)/theta2_E_fjt_firm_l
bysort fid: gen diff_tax_emi=(tax_emi-tax_emi_l)/tax_emi_l


* only keep observations in 2005
keep if indc_y==2
bysort prod_2: egen m_elasticity=pctile(diff_theta2_E_fjt_firm),p(50)	
bysort prod_2: egen m_MC=pctile(diff_MC),p(50)	
bysort prod_2: egen m_markup=pctile(diff_markup),p(50)
bysort prod_2: egen m_tax_emi=pctile(diff_tax_emi),p(50)

*---------------------------------------------------------
* 7. Columns 7  in Table 2: changes in implied emission tax
*------------------------------------------------------------

capture drop ub_tax lb_tax
egen ub_tax=pctile(diff_tax_emi),p(97)
egen lb_tax=pctile(diff_tax_emi),p(3)
bysort prod_2: sum diff_tax_emi   if diff_tax_emi>lb_tax&diff_tax_emi<ub_tax         /*mean change in emission tax*/

*---------------------------------------------------------
* 8. Columns 8  in Table 2: changes in implied emission tax
*------------------------------------------------------------

bysort prod_2: sum m_tax_emi


*************************************************************************************************************************************











*************************************Table 3: Markups***************************************************************88
*************************************Table 3: Markups***************************************************************88
*************************************Table 3: Markups***************************************************************88

*---------------------------------------------
*Part I:
*implied markup from Single-technology
*----------------------------------------------

use "D:\Nanjing\2019\decomposition_data_file\code for submission\Single_technology",clear

*------------------------------------------
*1. Mean level of Markup: Column 1 of Table 3
*------------------------------------------
bysort prod_2: sum Markup_firm

*------------------------------------------
*2. Mean level of Markup: Column 2 of Table 3
*------------------------------------------
bysort prod_2: egen m_Markup_firm=pctile(Markup_firm),p(50)
bysort prod_2: sum m_Markup_firm



*------------------------------------------------------------------
*This part is to calculate the change of Markup during 2000-2005
*-----------------------------------------------------------------

use "D:\Nanjing\2019\decomposition_data_file\code for submission\Single_technology",clear

egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
duplicates drop fid indc_y, force

xtset fid indc_y
tsfill, full
bysort fid: gen MC_fp_l=l.MC_fp                             /*lagged marginal cost*/
bysort fid: gen Markup_firm_l=l.Markup_firm                 /*lagged markup*/
bysort fid: gen theta2_E_fjt_firm_l=l.theta2_E_fjt_firm     /*lagged elasticity of emission*/
bysort fid: gen tax_emi_l=l.tax_emi                        /*lagged implied emission tax*/

bysort fid: gen diff_MC=(MC_fp-MC_fp_l)/MC_fp_l
bysort fid: gen diff_markup=(Markup_firm-Markup_firm_l)/Markup_firm_l
bysort fid: gen diff_theta2_E_fjt_firm=(theta2_E_fjt_firm-theta2_E_fjt_firm_l)/theta2_E_fjt_firm_l
bysort fid: gen diff_tax_emi=(tax_emi-tax_emi_l)/tax_emi_l


* only keep observations in 2005
keep if indc_y==2
bysort prod_2: egen m_elasticity=pctile(diff_theta2_E_fjt_firm),p(50)	
bysort prod_2: egen m_MC=pctile(diff_MC),p(50)	
bysort prod_2: egen m_markup=pctile(diff_markup),p(50)
bysort prod_2: egen m_tax_emi=pctile(diff_tax_emi),p(50)

*-------------------------------------------------------------------------
* 3. Columns 3 of Table 3: markup changes
*-------------------------------------------------------------------------

capture drop ub_mp lb_mp
egen ub_mp=pctile(diff_markup),p(97)
egen lb_mp=pctile(diff_markup),p(3)
bysort prod_2: sum diff_markup   if diff_markup>lb_mp&diff_markup<ub_mp         /*mean change in markup*/

*---------------------------------------------
* 4. Columns 4 of Table 3
**********************************************
bysort prod_2: sum m_markup







************************************Multiple-Technology****************************************
*---------------------------------------------
*Part II:
*implied markup from Multiple-technology
*----------------------------------------------



use "D:\Nanjing\2019\decomposition_data_file\code for submission\Multiple_technology",clear

gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace prod_2=. if prod_2==9
drop if prod_2==.
***************************************************************************************************
*-----------------------------------------
*5. Column 5 of Table 3
*-----------------------------------------

bysort prod_2: sum Markup_firm

*----------------------------------------------------------------------
*6. column 6 of Table 3
*-----------------------------------------------------------
bysort prod_2: egen m_Markup_firm=pctile(Markup_firm),p(50)
bysort prod_2: sum m_Markup_firm




*****************************************Split here****************************************


*----------------------------------------------------------
*  This part is to calculate the change during 2000-2005
*----------------------------------------------------------

use "D:\Nanjing\2019\decomposition_data_file\code for submission\Multiple_technology",clear

gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace prod_2=4 if prod_2==9

egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
*keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
drop if Markup_firm<0
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
sort fid svshare indc_y
duplicates drop fid indc_y, force

xtset fid indc_y
tsfill, full

bysort fid: gen MC_fp_l=l.MC_fp                             /*lagged marginal cost*/
bysort fid: gen Markup_firm_l=l.Markup_firm                 /*lagged markup*/
bysort fid: gen theta2_E_fjt_firm_l=l.theta2_E_fjt_firm     /*lagged elasticity of emission*/
bysort fid: gen tax_emi_l=l.tax_emi                        /*lagged implied emission tax*/

bysort fid: gen diff_MC=(MC_fp-MC_fp_l)/MC_fp_l
bysort fid: gen diff_markup=(Markup_firm-Markup_firm_l)/Markup_firm_l
bysort fid: gen diff_theta2_E_fjt_firm=(theta2_E_fjt_firm-theta2_E_fjt_firm_l)/theta2_E_fjt_firm_l
bysort fid: gen diff_tax_emi=(tax_emi-tax_emi_l)/tax_emi_l


* only keep observations in 2005

keep if indc_y==2
bysort prod_2: egen m_elasticity=pctile(diff_theta2_E_fjt_firm),p(50)	
bysort prod_2: egen m_MC=pctile(diff_MC),p(50)	
bysort prod_2: egen m_markup=pctile(diff_markup),p(50)
bysort prod_2: egen m_tax_emi=pctile(diff_tax_emi),p(50)

*---------------------------------------------------------------------------------
*7. Columns 7  in Table 3
*---------------------------------------------------------------------------------
capture drop ub_mp lb_mp
egen ub_mp=pctile(diff_markup),p(95)
egen lb_mp=pctile(diff_markup),p(5)

bysort prod_2: sum diff_markup   if diff_markup>lb_mp&diff_markup<ub_mp         /*mean change in markup*/

*-------------------------------------------
*8. Columns 8  in Table 3
*--------------------------------------------
bysort prod_2: sum m_markup  



































